package cn.bocweb.spinning.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ProductList2Bean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.fragment.CompanySearchResultFragment;
import cn.bocweb.spinning.view.fragment.ProductSearchResultFragment;

public class SearchResultActivity extends BaseActivity implements View.OnClickListener {

    ProductSearchResultFragment productSearchResultFragment;
    CompanySearchResultFragment companySearchResultFragment;
    List<String> list;
    int type;
    String city = "", cate = "", keyword, userid;
    UserPresenter presenter;

    @Bind(R.id.edit_content)
    EditText editContent;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.search_result_layout)
    FrameLayout searchResultLayout;
    @Bind(R.id.search_result_select)
    LinearLayout searchResultSelect;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_result);
        ButterKnife.bind(this);

        productSearchResultFragment = new ProductSearchResultFragment();

        presenter = new UserPresenterImp();

        UserBean.DataEntity dataEntity = new UserBean.DataEntity();
        dataEntity.readInfoFromLocal(this);
        userid = dataEntity.getUserid();

        type = getIntent().getIntExtra(Constant.TO_SEARCH_RESULT, 0);
        keyword = getIntent().getStringExtra(Constant.TO_SEARCH_KEYWORD);
        keyword = keyword == null ? "" : keyword;

        initToolbar();
        initClickBtn();
        initEditText();

        if (type == Constant.COMPANY) {
            companySearchResultFragment = new CompanySearchResultFragment();
            getSupportFragmentManager().beginTransaction().add(R.id.search_result_layout, companySearchResultFragment).commit();
            editContent.setText(keyword);
            companySearchResultFragment.requestCompanySearch(city, cate, keyword, userid);
        }
        if (type == Constant.PRODUCT) {
            productSearchResultFragment = new ProductSearchResultFragment();
            getSupportFragmentManager().beginTransaction().add(R.id.search_result_layout, productSearchResultFragment).commit();
            editContent.setText(keyword);
            productSearchResultFragment.requestProductSearch(city, cate, keyword, userid);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Constant.SEARCH_SCREEN) {
            city = data.getStringExtra(Constant.SEARCH_SCREEN_ADDRESS);
            cate = data.getStringExtra(Constant.SEARCH_SCREEN_CATE);

            city = city == null || city.isEmpty() ? "" : city;
            cate = cate == null || cate.isEmpty() ? "" : cate;

            if (type == Constant.COMPANY) {
                companySearchResultFragment.requestCompanySearch(city, cate, keyword, userid);
            }

            if (type == Constant.PRODUCT) {
                productSearchResultFragment.requestProductSearch(city, cate, keyword, userid);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_result_select: {
                Intent intent = new Intent(SearchResultActivity.this, ScreeningActivity.class);
                intent.putExtra(Constant.TO_SCREENING, type);
                if (!city.isEmpty()) {
                    intent.putExtra(Constant.TO_SCREENING_CITY, city);
                }
                if (!cate.isEmpty()) {
                    intent.putExtra(Constant.TO_SCREENING_CATE, cate);
                }
                startActivityForResult(intent, Constant.SEARCH_SCREEN);
                break;
            }
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    public void onEventMainThread(ProductList2Bean bean) {
        if (bean.getReturn_code() == 0) {
            productSearchResultFragment.setList(bean.getData());
        } else {
            productSearchResultFragment.clearList();
        }
    }



    private void initClickBtn() {
        searchResultSelect.setOnClickListener(this);
    }

    private void initToolbar() {
        toolbar.setNavigationIcon(R.mipmap.search_result_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initEditText() {
        editContent.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE){
                    if (type == Constant.COMPANY) {
                        companySearchResultFragment.requestCompanySearch(city, cate, keyword, userid);
                    }
                    if (type == Constant.PRODUCT) {
                        productSearchResultFragment.requestProductSearch(city, cate, keyword, userid);
                    }
                }
                return false;
            }
        });

        editContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                keyword = s.toString();
                if (type == Constant.COMPANY) {
                    companySearchResultFragment.requestCompanySearch(city, cate, keyword, userid);
                }
                if (type == Constant.PRODUCT) {
                    productSearchResultFragment.requestProductSearch(city, cate, keyword, userid);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

}
